package com.kakarote.hrm.controller;

import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSONObject;
import com.kakarote.common.log.annotation.OperateLog;
import com.kakarote.common.log.entity.OperationLog;
import com.kakarote.common.log.entity.OperationResult;
import com.kakarote.common.log.enums.ApplyEnum;
import com.kakarote.common.log.enums.BehaviorEnum;
import com.kakarote.common.log.enums.OperateObjectEnum;
import com.kakarote.common.log.enums.OperateTypeEnum;
import com.kakarote.core.common.Result;
import com.kakarote.core.entity.BasePage;
import com.kakarote.core.entity.UploadEntity;
import com.kakarote.hrm.entity.BO.QueryRecruitStartPositionPageListBO;
import com.kakarote.hrm.entity.PO.HrmRecruitStartPosition;
import com.kakarote.hrm.entity.VO.RecruitStartPositionVO;
import com.kakarote.hrm.service.IHrmRecruitStartPositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 招聘启动岗位 前端控制器
 * </p>
 *
 * @author system
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/hrmRecruitStartPosition")
@Api(tags = "招聘管理-招聘启动岗位")
public class HrmRecruitStartPositionController {

    @Autowired
    private IHrmRecruitStartPositionService recruitStartPositionService;

    @ApiOperation("添加招聘启动岗位")
    @PostMapping("/addRecruitStartPosition")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_START_POSITION, behavior = BehaviorEnum.SAVE)
    public Result addRecruitStartPosition(@Validated @RequestBody HrmRecruitStartPosition recruitStartPosition) {
        OperationLog operationLog = recruitStartPositionService.addRecruitStartPosition(recruitStartPosition);
        return OperationResult.ok(operationLog);
    }

    @ApiOperation("修改招聘启动岗位")
    @PostMapping("/setRecruitStartPosition")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_START_POSITION, behavior = BehaviorEnum.UPDATE)
    public Result setRecruitStartPosition(@Validated @RequestBody HrmRecruitStartPosition recruitStartPosition) {
        OperationLog operationLog = recruitStartPositionService.setRecruitStartPosition(recruitStartPosition);
        return OperationResult.ok(operationLog);
    }

    /**
     * 获取招聘启动岗位详情
     *
     * @param startPositionId 启动岗位id
     */
    @ApiOperation("获取招聘启动岗位详情")
    @PostMapping("/queryById/{startPositionId}")
    public Result<RecruitStartPositionVO> queryById(@PathVariable("startPositionId") Long startPositionId) {
        RecruitStartPositionVO recruitStartPositionVO = recruitStartPositionService.queryById(startPositionId);
        return Result.ok(recruitStartPositionVO);
    }

    @ApiOperation("查询招聘启动岗位列表")
    @PostMapping("/queryRecruitStartPositionPageList")
    public Result<BasePage<RecruitStartPositionVO>> queryRecruitStartPositionPageList(@RequestBody QueryRecruitStartPositionPageListBO queryRecruitStartPositionPageListBO) {
        BasePage<RecruitStartPositionVO> page = recruitStartPositionService.queryRecruitStartPositionPageList(queryRecruitStartPositionPageListBO);
        return Result.ok(page);
    }

    @ApiOperation("根据计划id查询所有启动岗位列表")
    @PostMapping("/queryByPlanId/{planId}")
    public Result<List<RecruitStartPositionVO>> queryByPlanId(@PathVariable("planId") Long planId) {
        List<RecruitStartPositionVO> positionList = recruitStartPositionService.queryByPlanId(planId);
        return Result.ok(positionList);
    }

    @ApiOperation("根据记录id查询所有启动岗位列表")
    @PostMapping("/queryByRecordId/{recordId}")
    public Result<List<RecruitStartPositionVO>> queryByRecordId(@PathVariable("recordId") Long recordId) {
        List<RecruitStartPositionVO> positionList = recruitStartPositionService.queryByRecordId(recordId);
        return Result.ok(positionList);
    }

    @ApiOperation("删除招聘启动岗位")
    @PostMapping("/deleteRecruitStartPosition/{startPositionId}")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_START_POSITION, behavior = BehaviorEnum.DELETE)
    public Result deleteRecruitStartPosition(@PathVariable("startPositionId") Long startPositionId) {
        OperationLog operationLog = recruitStartPositionService.deleteRecruitStartPosition(startPositionId);
        return OperationResult.ok(operationLog);
    }

    @ApiOperation("批量删除招聘启动岗位")
    @PostMapping("/batchDeleteRecruitStartPosition")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_START_POSITION, behavior = BehaviorEnum.DELETE)
    public Result batchDeleteRecruitStartPosition(@RequestBody List<Long> startPositionIds) {
        OperationLog operationLog = recruitStartPositionService.batchDeleteRecruitStartPosition(startPositionIds);
        return OperationResult.ok(operationLog);
    }

    @ApiOperation("根据计划id删除所有启动岗位")
    @PostMapping("/deleteByPlanId/{planId}")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_START_POSITION, behavior = BehaviorEnum.DELETE)
    public Result deleteByPlanId(@PathVariable("planId") Long planId) {
        OperationLog operationLog = recruitStartPositionService.deleteByPlanId(planId);
        return OperationResult.ok(operationLog);
    }

    @ApiOperation("根据记录id删除所有启动岗位")
    @PostMapping("/deleteByRecordId/{recordId}")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_START_POSITION, behavior = BehaviorEnum.DELETE)
    public Result deleteByRecordId(@PathVariable("recordId") Long recordId) {
        OperationLog operationLog = recruitStartPositionService.deleteByRecordId(recordId);
        return OperationResult.ok(operationLog);
    }

    @PostMapping("/exportRecruitStartPosition")
    @ApiOperation("导出招聘启动岗位")
    @OperateLog(apply = ApplyEnum.HRM, object = OperateObjectEnum.HRM_RECRUIT_START_POSITION, behavior = BehaviorEnum.EXCEL_EXPORT, type = OperateTypeEnum.EXPORT)
    public void exportRecruitStartPosition(@RequestBody QueryRecruitStartPositionPageListBO queryRecruitStartPositionPageListBO, HttpServletResponse response) throws IOException {
        recruitStartPositionService.exportRecruitStartPosition(response, queryRecruitStartPositionPageListBO);
    }

    @PostMapping("/downloadExcel")
    @ApiOperation("获取导入模板")
    public void downloadExcel(HttpServletResponse response) throws IOException {
        recruitStartPositionService.downloadExcel(response);
    }

    @PostMapping("/excelImport")
    @ApiOperation("excel导入招聘启动岗位")
    @OperateLog(apply = ApplyEnum.HRM, behavior = BehaviorEnum.EXCEL_IMPORT, type = OperateTypeEnum.IMPORT, object = OperateObjectEnum.HRM_RECRUIT_START_POSITION)
    public Result<JSONObject> excelImport(@RequestParam("file") MultipartFile file) {
        JSONObject object = recruitStartPositionService.excelImport(file);

        UploadEntity uploadEntity = recruitStartPositionService.uploadTempFile(file, null);

        OperationLog operationLog = new OperationLog();
        operationLog.setOperationObject("uploadData", uploadEntity.getFileId());
        operationLog.setOperationObject("name", "招聘启动岗位");

        JSONObject info = new JSONObject();
        info.put("count", object.getInteger("totalSize"));
        info.put("errorSize", object.getInteger("errSize"));
        info.put("errorFile", object.getString("token"));
        operationLog.setOperationInfo(info.toJSONString());
        return OperationResult.ok(object, ListUtil.toList(operationLog));
    }
}
